package semant.first_pass;

import semant.Env;
import symbol.Symbol;
import symbol.ClassInfo;
import symbol.MethodInfo;
import syntaxtree.MainClass;
import syntaxtree.VisitorAdapter;

final class MainClassHandler extends VisitorAdapter {

    private Env env;

    private MainClassHandler(Env e){
	super();
	env = e;
    }

    static void firstPass(Env e, MainClass mc){
	MainClassHandler h = new MainClassHandler(e);
	mc.accept(h);
    }

    public void visit(MainClass node){
    	Symbol name = Symbol.symbol(node.className.s);
    	ClassInfo cinfo = new ClassInfo(name);

	// insere classe principal na tabela de classes    	
    	if(!env.classes.put(name, cinfo)){
	    env.err.Error(node, new Object[]{"Nome da classe redefinido",
					     "Simbolo: " + name});
    	}

	// insere metodo 'main' na tabela de metodos da classe principal
	/*
	Symbol main = Symbol.symbol("_minijava_main_1");
        MethodInfo minfo = new MethodInfo(null, main, cinfo.name);
        if(!cinfo.addMethod(minfo)){
	    MethodInfo old = cinfo.methods.get(name);
	    if(old == null) throw new Error(); //Ver mensagem a ser atribuida
	    env.err.Error(node, new Object[]{"Metodo \'" + name + "' previamente declarado na classe \'" + cinfo.name + "\' aqui: [" + old.type.line + "," + old.type.row + "]"});
        }
	*/
    }

}
